﻿

function manageFormCommit(obj, acitonurl, $msg, $refreshTable) {
    $msg.html("");
    var parentForm = getBelongFormElement($(obj));
    if (parentForm.length > 0) {
        $(parentForm).ajaxSubmit({
            url: acitonurl,
            type: "post",
            success: function (data) {
                submitFormCreateCallback(data, $msg, $refreshTable);

            },
            error: function (msg) {
                $msg.html(msg);
            }
        });
    }

}

function submitFormCreateCallback(data, $msg, $refreshTable) {

    var fromData = eval(data);
    $("span[data-valmsg-for]").html("");
    $(fromData).each(function (index) {
        //debugger
        var dataRow = fromData[index];
        $("span[data-valmsg-for='" + dataRow.FieldName + "']").html(dataRow.Message);
        $("span[data-valmsg-for='" + dataRow.FieldName + "']").removeClass("field-validation-valid");
        $("span[data-valmsg-for='" + dataRow.FieldName + "']").addClass("field-validation-error");
        toastr.options = {
            "closeButton": true,
            "debug": false,
            "positionClass": "toast-bottom-center",
            "onclick": null,
            "showDuration": "1000",
            "hideDuration": "1000",
            "timeOut": "4000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }
        if (dataRow.FieldName == "ChangeResult") {
           
            if (dataRow.Code == "m001") {
                Command: toastr['success'](dataRow.Message, "Message Notifications");               
                $refreshTable.bootstrapTable('refresh', { silent: true });
                $("button[data-dismiss='modal']").click();
            } else {
                Command: toastr['error'](dataRow.Message, "Message Notifications");
            }
        } else {
            Command: toastr['filed']("失败", "Message Notifications");
        }
    });
}


function selectedRowChangeAvailable(pkey, posturl, $table, activate) {   
    var arrSelectedIds = Array();
    $.map($table.bootstrapTable('getSelections'), function (row) {
            arrSelectedIds.push(row[pkey]);
    });
    if (arrSelectedIds.length <= 0) {
        messageShow("未选择任何数据！")
        return;
    }
    toastr.options = {
        "closeButton": true,
        "debug": false,
        "positionClass": "toast-bottom-center",
        "onclick": null,
        "showDuration": "1000",
        "hideDuration": "1000",
        "timeOut": "4000",
        "extendedTimeOut": "1000",
        "showEasing": "swing",
        "hideEasing": "linear",
        "showMethod": "fadeIn",
        "hideMethod": "fadeOut"
    }

    $.ajax({
        type: "post",
        url: posturl,
        data: { primarykeys: arrSelectedIds.toString(), activate: activate },
        dataType: "json",
        success: function (data) {
            Command: toastr['success'](data.Message, "Message Notifications");
            if (data.Code == "m001") {
                $table.bootstrapTable('refresh', { silent: true });
            }
        },
        error: function (mes) {
            Command: toastr['success'](mes.responseText, "Message Notifications");
        }
    });



}





//默认为查找Form
function getBelongFormElement($element, findElementName) {

    if (findElementName == undefined || findElementName == "") {
        findElementName = "FORM";
    }
    findElementName = findElementName.toUpperCase();

    var parentNode = $element;
    var repeat = 0;

    while (parentNode.length > 0 && repeat < 50) {

        if (findElementName == parentNode.get(0).tagName) {
            //debugger
            return parentNode;
        }
        parentNode = parentNode.parent();
        repeat++;
    }
    return null;
}

